<?php

namespace frontend\modules\business\controllers\user;

use common\components\Upload;
use common\models\business\BusinessPig;
use common\models\farmerhouse\FarmerhouseHouse;
use common\models\shop\ShopHouseOrder;
use common\models\shop\ShopUserAddress;
use frontend\controllers\BaseMobileController;
use yii\helpers\Url;


/**
 *
 */
class FarmerHouseOrderController extends BaseMobileController
{



    /**
     * @return string
     * 农家乐订单
     */
    public function actionIndex()
    {
        $status = \Yii::$app->request->get('status');
        if(!is_null($status)){
            $orderlist = ShopHouseOrder::find()
                ->alias('o')
                ->leftJoin(FarmerhouseHouse::tableName().'as f','f.id=o.houseid')
                ->select('o.cost,o.ifcomment,o.iftravel,o.ordersn as no,o.days,o.roomnum,o.id,o.status,o.price,o.createtime,f.id as hid,f.name as title,f.envphoto as img')
                ->where('o.userid=:userid and o.status>=0 and o.status=:status and (o.ifcomment=0 || o.iftravel=0)',[':userid'=>$this->user['userid'],':status'=>\Yii::$app->request->get('status')])
                ->asArray()
                ->orderBy('status')
                ->all();
        }else{
            $orderlist = ShopHouseOrder::find()
                ->alias('o')
                ->leftJoin(FarmerhouseHouse::tableName().'as f','f.id=o.houseid')
                ->select('o.cost,o.ifcomment,o.iftravel,o.ordersn as no,o.days,o.roomnum,o.id,o.status,o.price,o.createtime,f.id as hid,f.name as title,f.envphoto as img')
                ->where('o.userid=:userid and o.status>=0 ',[':userid'=>$this->user['userid']])
                ->asArray()
                ->orderBy('status')
                ->all();
        }

        foreach ($orderlist as $key => $row){
            $totalnum = 0;
            $orderlist[$key]['oldstatus'] = $row['status'];
            $orderlist[$key]['status'] = ShopHouseOrder::$statues[$row['status']];
            $orderlist[$key]['num'] = $row['roomnum'].'间'.'/'.$row['days'].'天';
            $orderlist[$key]['totalprice'] = $row['price'];
            $orderlist[$key]['cost'] = $row['cost'];
            $orderlist[$key]['img'] = Upload::checkGetUrl($row['img'],false,'img');
        }


        return $this->render('@template/mobile/houseorder_list.html',[
            'orderlist' => \GuzzleHttp\json_encode($orderlist),
            'status' => $status
        ]);
    }

    /**
     * @return array
     * 取消订单
     */
    public function actionCancal()
    {
        $postdata = \Yii::$app->request->post();
        $res = \Yii::$app->db->createCommand()->update(ShopHouseOrder::tableName(),['status'=>ShopHouseOrder::STATUS_CANCAL],'id=:id',[':id'=>$postdata['id']])
            ->execute();
        return $res?$this->jsonMsg():$this->jsonMsg('取消失败',1);
    }


    /**
     * @return array
     * 确认入住
     */
    public function actionConfirmLive()
    {
        $postdata = \Yii::$app->request->post();
        $res = \Yii::$app->db->createCommand()->update(ShopHouseOrder::tableName(),['status'=>ShopHouseOrder::STATUS_CONFIRMLIVE],'id=:id',[':id'=>$postdata['id']])
            ->execute();
        return $res?$this->jsonMsg():$this->jsonMsg('网络异常',1);
    }


}